//
//  DWDTabBarItem.swift
//  dwdWb
//
//  Created by Dengdwd on 15/10/15.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit

class DWDTabBarItem: UITabBarItem {

    override var badgeValue:String? {
        didSet{
            
    let target = self.valueForKeyPath("_target") as! UITabBarController
            
    for childView in target.tabBar.subviews {
        if childView.isKindOfClass(NSClassFromString("UITabBarButton")!){
            for childBtn in childView.subviews {
                if childBtn.isKindOfClass(NSClassFromString("_UIBadgeView")!){
                    for childBadge in childBtn.subviews {
                        if childBadge.isKindOfClass(NSClassFromString("_UIBadgeBackground")!){
                            var count: UInt32 = 0
                            //                                拿到成员变量
                            let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!, &count)
                            //                                遍历找出_image属性
                            for i in 0..<count {
                                let ivar = ivars[Int(i)]
                                let ivarName = NSString(CString: ivar_getName(ivar), encoding: NSUTF8StringEncoding)
                                if ivarName!.isEqualToString("_image"){
                                    childBadge.setValue(UIImage(named: "main_badge"), forKeyPath:(ivarName! as String))
                                }
                            }
                        }
                    }
                }
            }
        }
            }
        }
    }
}
